using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using ProxyServer.EntityFrameworkCore;
using ProxyServer.EntityFrameworkCore.Npgsql;


namespace ProxyServer;

public class NpgsqlDesignTimeFactory : AbstractDesignTimeFactory
{
    public NpgsqlDesignTimeFactory() : base()
    {
    }

    public NpgsqlDesignTimeFactory(IConfiguration configuration) : base(configuration)
    {
    }

    public NpgsqlDesignTimeFactory(string[] args) : base(args)
    {
    }

    public override AppDbContext CreateDbContext(string[] args)
    {
        var connectionString = BuildConfiguration(args).GetConnectionString(DatabaseProviderNames.Npgsql);

        ArgumentNullException.ThrowIfNullOrWhiteSpace(connectionString, nameof(connectionString));
        Console.WriteLine("{0}: {1}", DatabaseProviderNames.Npgsql, connectionString);

        var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
        optionsBuilder.UseNpgsql(connectionString, npgsql => npgsql.MigrationsAssembly("ProxyServer.Npgsql"));
        
        return new AppDbContext(optionsBuilder.Options, new ModelBuilderOptions<AppDbContext>(NpgsqlModelBuilder.ConfigureModel));
    }
}